import java.util.Arrays;

/**
 * @author LKQ
 * @date 2021/12/27 11:13
 * @description 双指针
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {-7,-3,2,3,11};
        System.out.println(Arrays.toString(solution.sortedSquares(nums)));
    }

    public int[] sortedSquares(int[] nums) {
        int n = nums.length;
        // 新开辟一个存储空间，然后放入最大值。
        int[] ans = new int[n];
        for (int i = 0, j = n - 1, pos = n - 1; i <= j;) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                ans[pos] = nums[i] * nums[i];
                ++i;
            } else {
                ans[pos] = nums[j] * nums[j];
                --j;
            }
            --pos;
        }
        return ans;
    }

}
